#!/usr/bin/python
# -*- coding: utf-8 -*-
# waterlevel-measure.py -s 350301 -i F:\水尺识别测试图片\test
import utils
from os import stat, path
import time
import argparse
import pymssql

if __name__ == '__main__':
    # 设置参数
    ap = argparse.ArgumentParser()
    ap.add_argument("-s", "--stcd", required=True,
                    help="path to the input image")
    ap.add_argument("-i", "--image", required=True,
                    help="path to the input image")

    args = vars(ap.parse_args())

    # print('--------agrs', args['stcd'], args['image'])

    stcd = args['stcd']
    F = []
    utils.listdir(args['image'], F)
    measuerList = []

    for f in F:
        # statinfo = stat(f)
        # localTime = time.localtime(statinfo.st_atime)
        # strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime)
        fileNameWithoutExt = path.split(f)[1].split('.')[0].split('_')
        localTime = time.strptime(fileNameWithoutExt[0], "%Y-%m-%dT%H-%M-%S")
        strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime)
        waterRulerLevelCm = float(fileNameWithoutExt[1])
        measuerList.append((stcd, f, waterRulerLevelCm,
                            strTime, strTime))

    if len(measuerList) == 0:
        print('No Data')
        exit()

    print(measuerList)

    server = "strongiot.cn"  # 连接服务器地址
    user = "strong"  # 连接帐号
    password = "strong9889"  # 连接密码

    conn = pymssql.connect(server, user, password, "PicDB")  # 获取连接
    cursor = conn.cursor()  # 获取光标

    cursor.executemany(
        "INSERT INTO [PictureDiagnosis]([stcd], [destPath], [waterRulerLevelCm], [picDate], [creDate]) VALUES (%s, %s, %d, %s, %s)",
        measuerList)
    # 你必须调用 commit() 来保持你数据的提交如果你没有将自动提交设置为true
    conn.commit()
    conn.close()

    print('Import Success...')
